package org.colafries.admin.service.impl.index;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.colafries.admin.entity.domain.index.DataDomain;
import org.colafries.admin.entity.vo.index.DataDomainReqVO;
import org.colafries.admin.exceptions.ServiceException;
import org.colafries.admin.mapper.index.DataDomainMapper;
import org.colafries.admin.service.impl.IBaseServiceImpl;
import org.colafries.admin.service.index.IDataDomainService;
import org.colafries.common.snowflake.SnowFlake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 数据领域 服务实现类
 * </p>
 *
 * @author Ann
 * @since 2024-06-03
 */
@Service("DataDomainServiceImpl")
public class IDataDomainServiceImpl extends IBaseServiceImpl<DataDomainMapper, DataDomain, DataDomainReqVO> implements IDataDomainService {

    @Autowired
    DataDomainMapper dataDomainMapper;

    @Override
    public List<DataDomain> listAll(DataDomainReqVO dataDomainReqVO) {
        return super.listAll(dataDomainReqVO);
    }

    @Override
    public int add(DataDomainReqVO vo) {
        Long dataDomainCode = SnowFlake.nextId();
        DataDomain dataDomain = DataDomain.builder()
                .code(dataDomainCode)
                .busiCode(vo.getBusiCode())
                .name(vo.getName())
                .createUser(vo.getCreateUser())
                .updateUser(vo.getUpdateUser())
                .build();
        return dataDomainMapper.insert(dataDomain);
    }

    @Override
    public int delete(DataDomainReqVO vo) {
        DataDomain dataDomain = baseMapper.selectOne(Wrappers.<DataDomain>lambdaQuery().eq(DataDomain::getCode, vo.getCode()));
        if (dataDomain == null) {
            log.warn("数据领域不存在 code:" + vo.getCode());
            throw new ServiceException("数据领域不存在 code:" + vo.getCode());
        }
        return baseMapper.deleteById(dataDomain.getId());
    }

    @Override
    public DataDomain queryByCode(Long dataDomainCode){
        LambdaQueryWrapper<DataDomain> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DataDomain::getCode,dataDomainCode);
        return baseMapper.selectOne(queryWrapper);
    }
}
